package com.example.activity;

import java.text.SimpleDateFormat;
import java.util.Date;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.application.BaseActivity;
import com.example.db.DBService;
import com.example.zhaccount.MainActivity;
import com.example.zhaccount.R;

public class AddNoteActivity extends BaseActivity {
	private TextView tv_time;
	private Button btn_no,btn_yes;
	private EditText edit_title,edit_content;
	
	private DBService mdbService;
	private OnClickListener mClick;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_add_note);
		findView();
	}

	private void findView(){
		tv_time = (TextView) findViewById(R.id.time_tv);
		btn_no = (Button) findViewById(R.id.btn_cancle);
		btn_yes = (Button) findViewById(R.id.btn_save);
		edit_title = (EditText) findViewById(R.id.edit_title);
		edit_content = (EditText) findViewById(R.id.edit_context);
	
		String time = new SimpleDateFormat("yyyy-MM-dd").
				format(new Date(System.currentTimeMillis()));
		tv_time.setText(time);
		
		mClick = new mClick();
		btn_no.setOnClickListener(mClick);
		btn_yes.setOnClickListener(mClick);
		
		mdbService = new DBService(this);
	}
	
	
	class mClick implements OnClickListener{
		
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.btn_cancle:
				finish();
				break;
			case R.id.btn_save:
				saveNote();				//保存笔记
				break;
			default:
				break;
			}
		}
		
	}
	
	
	private void saveNote(){
		String title = edit_title.getText().toString();
		String content = edit_content.getText().toString();
		long num = mdbService.insertNote(title, content);
		if (num != -1) {
			finish();
			Intent itt = new Intent(this, MainActivity.class);
			itt.putExtra("pagerIndex", 1);
			startActivity(itt);
			Toast.makeText(this, "insert success!", Toast.LENGTH_SHORT).show();
		}else{
			Toast.makeText(this, "----fail-----", Toast.LENGTH_SHORT).show();
		}
	}
}
